<template>
	<view class="relative flex flex-col items-center pt-[10vh] gap-y-[20px]">
		<u-image :src="qrcode" width="160" height="180">
			<template #loading>
				<u-loading-icon></u-loading-icon>
			</template>
		</u-image>
		<text v-if="qrcode" class="text-[14px] mt-[10px]">分享商城链接/二维码</text>
		<u-button v-if="qrcode" @click="saveToAlbum" text="保存到相册" type="primary" size="normal" :customStyle="{borderRadius:'8px'}"></u-button>
	</view>
</template>

<script setup>
	import { ref } from "vue";
	import * as api from "@/api.js";
	import * as uniapp from "@dcloudio/uni-app";
	import {useUserStore} from "@/store/config.js";
	
	const store = useUserStore();
	const qrcode = ref();
	
	uniapp.onLoad(async() => {
		uni.showLoading();
		const result = await api.getQrCode(store.user.UserID);
		uni.hideLoading();
		console.log(result)
		if(result[0]) {
			qrcode.value = result[0];
		} else {
			uni.showModal({
				content:result[1],
				showCancel:false,
				success() {
					uni.navigateBack();
				}
			})
		}
	})
	
	const saveToAlbum = () => {
		uni.getImageInfo({
			src:qrcode.value,
			success(res) {
				console.log(res)
				if(res.errMsg == 'getImageInfo:ok' && res.path) {
					uni.saveImageToPhotosAlbum({
						filePath:res.path,
						success(res1) {
							uni.showToast({
								icon:'success',
								title:'保存成功'
							})
						},
						fail(err) {
							uni.showToast({
								icon:'none',
								title:JSON.stringify(err)
							})
						}
					})
				}
			}
		})
	}
</script>

<style>

</style>
